package com.hugedata.cdnserver.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: DownloadUtils
 * @Description: 下载工具
 * @author Wangxiaoming
 * @date Jul 7, 2014 9:10:50 PM
 *
 */
public class DownloadUtils
{
    private static final Logger LOGGER = LoggerFactory.getLogger(DownloadUtils.class);
    
    /**
     * 
    * @Title: httpDown
    * @Description: 将srcPath连接的文件内容下载到dstPath中(http下载)
    * @author Wangxiaoming
    * @param dstPath
    * @param srcPath
     */
    public static boolean httpDown(String dstPath, String srcPath)
    {
        LOGGER.debug("begin http down dstPath: {} \t srcPath: {}", dstPath, srcPath);
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(srcPath);
        CloseableHttpResponse response = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        InputStream instream = null;
        try
        {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null)
            {
                File file = new File(dstPath);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                int len;
                instream = entity.getContent();
                byte[] byteArr = new byte[1024];
                while ((len = instream.read(byteArr)) != -1)
                {
                    bos.write(byteArr, 0, len);
                }
                bos.flush();
            }
        }
        catch (ClientProtocolException e)
        {
            LOGGER.error("download Exception", e);
            return false;
        }
        catch (IOException e)
        {
            LOGGER.error("download Exception", e);
            return false;
        }
        finally
        {
            try
            {
                if (null != instream)
                {
                    instream.close();
                    instream = null;
                }
            }
            catch (IOException e)
            {
                LOGGER.error("download Exception", e);
            }
            
            try
            {
                if (null != bos)
                {
                    bos.close();
                    bos = null;
                }
            }
            catch (IOException e)
            {
                LOGGER.error("download Exception", e);
            }
            
            try
            {
                if (null != fos)
                {
                    fos.close();
                    fos = null;
                }
            }
            catch (IOException e)
            {
                LOGGER.error("download Exception", e);
            }
            
            try
            {
                if (null != response)
                {
                    response.close();
                    response = null;
                }
            }
            catch (IOException e)
            {
                LOGGER.error("download Exception", e);
            }
        }
        LOGGER.debug("end http down: {}", srcPath);
        return true;
    }
}
